<?php
session_start(); // 启动会话
include "../includes/common.php";
$title = "防红系统对接";
include "head.php";

if($islogin != 1) {
    exit("<script language='javascript'>window.location.href='./login.php';</script>");
}
?>

<div class="col-xs-12 col-sm-10 col-lg-8 center-block" style="float: none;" id="main">
<div class="panel panel-default">
    <div class="panel-heading font-bold text-center" style="background: linear-gradient(to right,#111111,#333333);">
        <h3 class="panel-title"><font color="#fff"><i class="fa fa-link"></i>&nbsp;&nbsp;<b>云集安全连接系统</b></font></h3>
    </div>
    <ul class="list-group no-radius">
        <li style="font-weight:bold" class="list-group-item">
            <!-- 表单开始 -->
            <form method="post" action="">
                <div class="form-group">
                    <label for="dwz_title">防红标题:</label>
                    <input type="text" class="form-control" id="dwz_title" name="dwz_title" placeholder="请输入防红网址标题" 
                           value="<?php echo htmlspecialchars(isset($_POST['dwz_title']) ? $_POST['dwz_title'] : '联宏卡店'); ?>" required>
                </div>
                <div class="form-group">
                    <label for="dwz_url">目标链接:</label>
                    <input type="url" class="form-control" id="dwz_url" name="dwz_url" placeholder="请输入防红目标链接" 
                           value="<?php echo htmlspecialchars(isset($_POST['dwz_url']) ? $_POST['dwz_url'] : 'https://vip.yunjii.cn/'); ?>" required>
                </div>
                <button type="submit" class="btn btn-primary btn-block">获得防红链接</button>
            </form>
            <!-- 表单结束 -->

            <!-- 显示当前会话期间生成的防红网址 -->
            <div class="panel panel-default">
			<!-- 显示调用结果信息 -->
                <div class="panel-body call-info">
				
				<?php
// 检查是否存在会话中的结果数组，如果不存在则初始化为空数组
if (!isset($_SESSION['generated_urls'])) {
    $_SESSION['generated_urls'] = [];
}

// 初始化一个变量用于存储调用信息
$call_info = '';

// 如果是POST请求，则处理表单提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // 获取表单数据
    $dwz_title = isset($_POST['dwz_title']) ? trim($_POST['dwz_title']) : '';
    $dwz_url = isset($_POST['dwz_url']) ? trim($_POST['dwz_url']) : '';

    // 检查必填项是否为空
    if (empty($dwz_title) || empty($dwz_url)) {
        $call_info = "<p class='call-message error'>请确保所有字段都已填写。</p>";
    } else {
        // 调用 req.php 以获取短链接
        include('req.php'); // 假设 req.php 在同一目录下
        
        // 创建请求参数
        $reqParams = array(
            'dwz_title' => $dwz_title,
            'dwz_dlws' => 5, // Key位数
            'dwz_type' => 1, // 访问限制
            'dwz_url' => $dwz_url,
            'api_key' => constant("api_key"),
            'sign' => getSignature($dwz_title, 1, $dwz_url, constant("api_key"), constant("api_secrete")) // 签名
        );

        // 调用并接收调用结果
        $curlRequestResult = curlRequest($reqUrl, $reqParams);

        // 解析JSON格式的响应数据
        $responseData = json_decode($curlRequestResult, true);

        // 检查状态码和消息
        if (isset($responseData['code']) && isset($responseData['msg'])) {
            if ($responseData['code'] == 200 && isset($responseData['url']) && !empty($responseData['url'])) {
                // 成功获取到短网址，静默处理，更新表格内容
                $shortUrl = htmlspecialchars($responseData['url']);
                
                // 将结果插入到会话的结果数组的最前面
                array_unshift($_SESSION['generated_urls'], [
                    'title' => $dwz_title,
                    'original_url' => $dwz_url,
                    'short_url' => $shortUrl,
                    'created_at' => date('Y-m-d H:i:s')
                ]);

                // 设置成功消息，包含新生成的短网址
                $call_info = "<p class='call-message success'>新防红网址已成功添加：<a href='{$shortUrl}' target='_blank'>{$shortUrl}</a></p>";
            } else {
                // 请求失败，设置错误信息
                $call_info = "<p class='call-message error'>生成防红网址时遇到问题，请稍后再试。</p>";
            }
        } else {
            // 如果没有找到 'code' 或 'msg' 键，则可能是解析失败或其他问题
            $call_info = "<p class='call-message error'>与服务器通信时出现问题，请检查您的网络连接。</p>";
        }
    }
}

// 处理清空防红链接的操作
if (isset($_POST['clear_urls'])) {
    $_SESSION['generated_urls'] = [];
    $call_info = "<p class='call-message success'>所有防红网址已成功清空。</p>";
}
?>

<?php echo $call_info; ?></div>
               <!-- <div class="panel-heading">本次会话生成的防红网址</div>-->
                

                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>标题</th>
                            <th>目标链接</th>
                            <th>防红网址</th>
                            <th>创建时间</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($_SESSION['generated_urls'] as $result): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($result['title']); ?></td>
                            <td><?php echo htmlspecialchars($result['original_url']); ?></td>
                            <td><a href="<?php echo htmlspecialchars($result['short_url']); ?>"><?php echo htmlspecialchars($result['short_url']); ?></a></td>
                            <td><?php echo htmlspecialchars($result['created_at']); ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>

                <!-- 清空防红链接按钮 -->
                <form method="post" action="" onsubmit="return confirm('确定要清空所有防红网址吗？')">
                    <button type="submit" class="btn btn-primary btn-block" name="clear_urls">清空防红链接</button>
                </form>
            </div>
        </li>
    </ul>
</div>
</div>
</body>
</html>



